package com.zeen.wima.common;

import java.util.HashMap;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import com.zeen.wima.R;

/**
 * @author jacky
 * @email: yanghf1985@hotmail.com
 * @brief: 仅以此程序献给那些默默无闻工作在第一线的贼们,祝愿你们早日修成正果
 *         希望此软件可以帮助你们尽快到达该去的地方，早日吃上国家提供的窝头，国家提供的住所... FUCK YOU, MAN
 */
public class Wima extends Application {

	String _IMEI;

	public static final String DOMAIN = "";
	
	private HashMap<String, Object> _proxys;

	static Wima _instance;

	static public Wima getInstance() {
		return _instance;
	}

	Tracer _tracer;

	LocalData _data;

	Ad _mAd;

	public void setLastAd(Ad ad) {
		_mAd = ad;
		sendNification(this, "com.zeen.iphone.ad.VIEW", R.drawable.noti_ic_cy,
				ad.getTitle(), ad.getDescription(),
				android.app.Notification.FLAG_AUTO_CANCEL,
				android.app.Notification.DEFAULT_VIBRATE, 1);
	}

	public Ad getLastAd() {
		return _mAd;
	}

	public LocalData getLocalData() {
		if (_data == null)
			_data = new LocalData(this.getApplicationContext());
		return _data;
	}

	public Tracer getTracer() {
		return _tracer;
	}

	public Wima() {
		super();
		_instance = this;
		_tracer = new Tracer();
		_proxys = new HashMap<String, Object>();
	}

	/**
	 * 取得 Proxy 实例
	 * 
	 * @return
	 */
	public Object getProxy(String name) {
		if (!_proxys.containsKey(name)) {
			try {
				Object object = Class.forName(name).newInstance();
				_proxys.put(name, object);
			} catch (Exception e) {
				return null;
			}
		}
		return _proxys.get(name);
	}

	/**
	 * 使用此值作为UID
	 * 
	 * @return
	 */
	public String getIMEI() {
		if (_IMEI == null) {
			TelephonyManager telephonyManager = (TelephonyManager) this
					.getSystemService(Context.TELEPHONY_SERVICE);
			if (telephonyManager != null)
				_IMEI = telephonyManager.getDeviceId();
			else
				_IMEI = "-";
		}
		return _IMEI;
	}

	
	/**
	 * 网站地址
	 * @return
	 */
	public String getDomain()
	{
		return "";
	}
	
	
	/**
	 * 通知
	 * 
	 * @param context
	 * @param action
	 * @param icon
	 * @param title
	 * @param contentText
	 * @param flag
	 * @param NDefault
	 * @param id
	 */
	public void sendNification(Context context, String action, int icon,
			CharSequence title, CharSequence contentText, int flag,
			int NDefault, int id) {
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

		long when = System.currentTimeMillis();
		Intent notificationIntent = new Intent(action);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		Notification notification = new Notification(icon, title, when);
		notification.defaults = NDefault;
		notification.setLatestEventInfo(context, title, contentText,
				contentIntent);
		notification.flags |= flag;
		mNotificationManager.notify(id, notification);
	}

	public void clearAllNitification(Context context) {
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancelAll();
	}

	public void clearNitification(Context context, int id) {
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancel(id);
	}
}
